/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shutdown;

import Listeners.MainMenuListener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Joel
 */
public class RedirectedFrame extends JFrame {

    JTextArea aTextArea = new JTextArea() {{
            setEditable(false);
            }};
    PrintStream sysOut = new PrintStream(new FilteredStream(
            new ByteArrayOutputStream(), aTextArea));
    PrintStream errOut = new PrintStream(new FilteredStream(
            new ByteArrayOutputStream(), null));
    private boolean logFile;

    RedirectedFrame(boolean logFile) {

        this.logFile = logFile;
        System.setOut(sysOut);
        System.setErr(errOut);
        setTitle("Auto Shutdown");
        setSize(500, 300);
        setLayout(new BorderLayout());
        add(new JScrollPane(aTextArea), BorderLayout.CENTER);
        Image image = Toolkit.getDefaultToolkit().createImage("ico2.jpg");
        setIconImage(image);
        setJMenuBar(new JMenuBar() {{
                add(new JMenu("File") {{
                    setMnemonic('F');
                    add(new JMenuItem("Open"){{
                        }});
                    addSeparator();
                    add(new JMenuItem("Exit"){{
                        addActionListener(MainMenuListener.getInstance());
                        }});
                    }});
                add(new JMenu("Help") {{
                    setMnemonic('H');
                    add(new JMenuItem("Basics"){{
                        addActionListener(MainMenuListener.getInstance());
                        }});
                    add(new JMenuItem("Start Logging"){{
                        addActionListener(MainMenuListener.getInstance());
                        }});
                    addSeparator();
                    add(new JMenuItem("About..."){{
                        addActionListener(MainMenuListener.getInstance());
                        }});
                    addActionListener(MainMenuListener.getInstance());
                    }});
            }});
        setScreenLocation();
        }

    public boolean isLogFile() {
        return logFile;
        }

    public void setLogFile(boolean logFile) {
        this.logFile = logFile;
        }

    class FilteredStream extends FilterOutputStream {

        private JTextArea aTextArea;

        public FilteredStream(OutputStream aStream, JTextArea ta) {
            super(aStream);
            aTextArea = ta;
            }

        @Override
        public void write(byte b[]) throws IOException {
            write(b, 0, b.length);
            }

        @Override
        public void write(byte b[], int off, int len) throws IOException {
            String aString = new String(b, off, len);
            if (aTextArea != null)
                aTextArea.append(aString);
            if (logFile) {
                FileWriter aWriter = new FileWriter("log.log", true);
                aWriter.write(aString);
                aWriter.close();
                }
            }
        }

    public void setScreenLocation() {
        Dimension dim = getToolkit().getScreenSize();
        Rectangle abounds = getBounds();
        setLocation((dim.width - abounds.width) / 2,
                (dim.height - abounds.height) / 2);
        }
    }
